/*
 * Copyright 2012 Sergey Netesanyi.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.versatile.gwt.serialization.client;

import com.google.gwt.core.client.GWT;

/**
 * Client-side object serializer.
 *
 * @author Sergey Netesanyi
 * @version 1.0
 */
public final class Serializer {
    /** Singleton-instance of {@link ISerializer}. */
    private static ISerializer serializer;

    /** Creates an instance of {@link Serializer}. */
    private Serializer() {
    }

    /**
     * Serializes object to string.
     *
     * @param object
     *         Object to serialize.
     * @return Serialized object.
     */
    public static String serialize(Object object) {
        return get().serialize(object);
    }

    /**
     * Deserializes object from string.
     *
     * @param encoded
     *         String representation of the object.
     * @return Deserialized object.
     */
    public static <T> T deserialize(String encoded) {
        return get().<T>deserialize(encoded);
    }

    /**
     * Checks if the target type is supported.
     *
     * @param clazz
     *         Type to check.
     * @return <code>true</code> - if an object of this type can be serialized, otherwise <code>false</code>.
     */
    public static boolean isSerializable(Class<?> clazz) {
        return get().isSerializable(clazz);
    }

    private static ISerializer get() {
        if (serializer == null) {
            serializer = GWT.create(ISerializer.class);
        }
        return serializer;
    }
}
